<?php
/**
 * Slipstream :: Highly adaptive dependency injection framework
 * Copyright 2010-2011 Ian Unruh (Xerof3ar)
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *   http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
namespace Slipstream\Activation\Contributor\Construction;

use ArrayObject;
use ReflectionClass;
use Slipstream\Infrastructure\Parameter\ResolverServiceInterface;
use Slipstream\Configuration\ParameterPolicy;
use Slipstream\Activation\ContributorInterface;
use Slipstream\Activation\SessionInterface;

/**
 *
 *
 * @author Ian Unruh (Xerof3ar) <ianunruh@gmail.com>
 * @since Class avaliable since release 1.0
 */
class SimpleConstructionContributor implements ContributorInterface
{
	/**
	 * @var ResolverServiceInterface
	 */
	protected $parameterResolver;
	
	/**
	 * Called during an activation session so it can be contributed to
	 *
	 * @param SessionInterface $session
	 * @return void
	 */
	public function onActivation(SessionInterface $session)
	{
		// @todo Replace with resolved type
		$classType = $session->getIdentifier();
		
		$reflection = new ReflectionClass($classType);
		$ctor = $reflection->getConstructor();
		
		if(is_null($ctor) || $ctor->getNumberOfParameters() == 0) {
			$session->setInstance($reflection->newInstance());
			return;
		}
		
		$injections = $this->parameterResolver->resolveParameters($ctor, $session);
		
		$instance = $reflection->newInstanceArgs($injections->getArrayCopy());
		$session->setInstance($instance);
	}
	
	/**
	 *
	 *
	 * @param ResolverServiceInterface $parameterResolver
	 * @return void
	 */
	public function setParameterResolver(ResolverServiceInterface $parameterResolver)
	{
		$this->parameterResolver = $parameterResolver;
	}
}